package com.thp.bigdata.thread.lock;

import java.util.ArrayList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 观察现象:一个线程获得锁后,另一个线程不会一直等待
 * @author lenovo
 *
 */
public class MyTryLock {
	
	
	private static ArrayList<Integer> arrayList = new ArrayList<Integer>();
	static Lock lock = new ReentrantLock();
	public static void main(String[] args) {
		new Thread() {
			public void run() {
				Thread thread = Thread.currentThread();
				boolean tryLock = lock.tryLock();
				System.out.println(thread.getName() + " " +tryLock);
				if(tryLock) {
					try {
						System.out.println(thread.getName() + " 得到了锁");
						for(int i = 0; i < 5; i++) {
							arrayList.add(i);
						}
					} catch (Exception e) {
						
					} finally {
						System.out.println(thread.getName() + "释放了锁...");
						lock.unlock();  // 需要释放锁
					}
				}
				
			};
		}.start();
		
		
		new Thread() {
			public void run() {
				Thread thread = Thread.currentThread();
				boolean tryLock  = lock.tryLock();
				System.out.println(thread.getName() + " " + tryLock);
				if(tryLock) {
					try {
						System.out.println(thread.getName() + "得到了锁");
						for(int i = 0; i < 5; i++) {
							arrayList.add(i);
						}
					} catch (Exception e) {
						// TODO: handle exception
					} finally {
						System.out.println(thread.getName() + "释放了锁");
						lock.unlock();
					}
				}
			};
		}.start();
		
		
		
	}
	
	
	
	
	
}
